import tkinter as tk
import time
import json
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator


plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['font.serif'] = ['KaiTi']
time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

#读取以往数据
with open("data.json","r") as f:
    data = json.load(f)
data["lastUpdateTime"] = time_now


window = tk.Tk()
window.title("评价工具")
window.geometry("350x300")


# 单选框1
v1 = tk.IntVar()

f1 = tk.Frame(window,width=100,height=20)
f1.grid(row=0,column=0)

l1 = tk.Label(f1,text="你今天的状态：")
l1.grid(row=0,column=0)

DATA1 = [("非常好",1),("好",2),("一般",3),("差",4),("很差",5)]

for data1,num in DATA1:
    r1 = tk.Radiobutton(f1, text=data1, value=num, variable=v1)
    r1.grid(row=0,column=num)

# 单选框2
f2= tk.Frame(window,width=100,height=20)
f2.grid(row=1,column=0)

l2 = tk.Label(f2,text="你今天上课的表现：")
l2.grid(row=0,column=0)

v2 = tk.IntVar()
DATA2 = [("A",1),("A-",2),("B",3),("B-",4),("Z",5)]

for data2,num in DATA2:
    r2 = tk.Radiobutton(f2, text=data2, value=num, variable=v2)
    r2.grid(row=0,column=num)

# 单选框3
f3= tk.Frame(window,width=100,height=20)
f3.grid(row=2,column=0)

l3 = tk.Label(f3,text="你今天课后的表现：")
l3.grid(row=0,column=0)

v3 = tk.IntVar()
DATA3 = [("A",1),("A-",2),("B",3),("B-",4),("Z",5)]

for data3,num in DATA3:
    r3 = tk.Radiobutton(f3, text=data3, value=num, variable=v3)
    r3.grid(row=0,column=num)


#输入框
f4= tk.Frame(window,width=100,height=20)
f4.grid(row=3,column=0)
l3 = tk.Label(f4,text="你今天冥想的最长时间（秒）：")
l3.grid(row=0,column=0)
e1 = tk.Entry(f4,width=20)
e1.grid(row=0,column=1)


def get_data():
    time_abbr = (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())).split(" ")[0]
    datas ={}
    datas["date"] = time_abbr
    datas["state"] = v1.get()
    datas["class"] = v2.get()
    datas["afterclass"] = v3.get()
    datas["meditation"] = e1.get()
   
    i=len(data)-2
    if data[str(i)]["date"] == time_abbr:
        i=len(data)-2
    else:
        i+=1

    
    data[str(i)] = datas    
    with open("data.json","w") as f:
        json.dump(data,f)


def draw_meditation():
    with open("data.json","r") as f:
        res = f.read()
        datas = json.loads(res)
    x = []
    y = []
    i=0
    
    for data in datas:
        if i != 0:
            x.append((datas[data]['date']))
            #print(type(data["meditation"]))
            y.append(int(datas[data]['meditation']))

        i+=1
    plt.figure(num = "冥想时间变化图")
    plt.xlabel("日期")
    plt.ylabel("时长/秒")
    x_major_locator=MultipleLocator(1)
    ax = plt.gca()
    ax.spines["top"].set_color("none")
    ax.spines["right"].set_color("none")
    ax.xaxis.set_ticks_position("bottom")
    ax.yaxis.set_ticks_position("left")

    plt.xlim(0,(len(x)+1))
    plt.ylim(0,100)
    plt.plot(x,y)
    plt.xticks(size='small',rotation=45)
    plt.show()


def draw_state():
    with open("data.json","r") as f:
        res = f.read()
        datas = json.loads(res)
    value = []
    lists= []
    i=0
    for data in datas:
        if i != 0:
            lists.append(int((datas[data]['state'])))
        i+=1
    a = b = c = d = e = 0
    for list in lists:
        if list ==1:
            a+=1
        elif list ==2:
            b+=1
        elif list ==3:
            c+=1
        elif list ==4:
            d+=1
        elif list ==5:
            e+=1
    value.append(a)
    value.append(b)
    value.append(c)
    value.append(d)
    value.append(e)


    label = ["非常好\n","好\n","一般\n","差\n","很差\n"]
    color = ["green","blue","yellow","orange","red"]
    plt.figure(figsize = (6,6),num = "每日状态统计图")
    plt.pie(value, colors = color,autopct='%1.1f%%',radius =0.8)
    plt.legend(label)
    plt.show()


def draw_class():
    with open("data.json","r") as f:
        res = f.read()
        datas = json.loads(res)
    value = []
    lists= []
    i=0
    for data in datas:
        if i != 0:
            lists.append(int((datas[data]['class'])))
        i+=1
    a = b = c = d = e = 0
    for list in lists:
        if list ==1:
            a+=1
        elif list ==2:
            b+=1
        elif list ==3:
            c+=1
        elif list ==4:
            d+=1
        elif list ==5:
            e+=1
    value.append(a)
    value.append(b)
    value.append(c)
    value.append(d)
    value.append(e)

    label = ["A","A-","B","B-","Z"]
    color = ["green","blue","yellow","orange","red"]
    plt.figure(figsize = (6,6),num = "每日课堂评价统计图")
    plt.pie(value, colors = color,autopct='%1.1f%%',radius =0.8)
    plt.legend(label)
    plt.show()

def draw_afterclass():
    with open("data.json","r") as f:
        res = f.read()
        datas = json.loads(res)
    value = []
    lists= []
    i=0
    for data in datas:
        if i != 0:
            lists.append(int((datas[data]['afterclass'])))
        i+=1
    a = b = c = d = e = 0
    for list in lists:
        if list ==1:
            a+=1
        elif list ==2:
            b+=1
        elif list ==3:
            c+=1
        elif list ==4:
            d+=1
        elif list ==5:
            e+=1
    value.append(a)
    value.append(b)
    value.append(c)
    value.append(d)
    value.append(e)

    label = ["A","A-","B","B-","Z"]
    color = ["green","blue","yellow","orange","red"]
    plt.figure(figsize = (6,6),num = "每日课后评价统计图")
    plt.pie(value, colors = color,autopct='%1.1f%%',radius =0.8)
    plt.legend(label)
    plt.show()


# 获取状态
b1 = tk.Button(window,width=40,text="点击存储今日数据",command=get_data)
b1.grid(row=4,column=0)

b2 = tk.Button(window,width=40,text="冥想时间变化图",command=draw_meditation)
b2.grid(row=5,column=0)

b3 = tk.Button(window,width=40,text="每日状态统计图",command=draw_state)
b3.grid(row=6,column=0)

b4 = tk.Button(window,width=40,text="每日课堂评价统计图",command=draw_class)
b4.grid(row=7,column=0)

b5 = tk.Button(window,width=40,text="每日课后评价统计图",command=draw_afterclass)
b5.grid(row=8,column=0)

window.mainloop()
